package com.lanswon.qzsmk.controller.cardManageController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lanswon.qzsmk.base.CardContants;
import com.lanswon.qzsmk.base.DwzPage;
import com.lanswon.qzsmk.exception.ApplicationException;
import com.lanswon.qzsmk.model.Cert;
import com.lanswon.qzsmk.model.FeeRules;
import com.lanswon.qzsmk.service.appManageService.AppFeeService;
import com.lanswon.qzsmk.service.cardManageService.ChangeCardService;
import com.lanswon.qzsmk.vo.ChangeCardInfo;
import com.lanswon.qzsmk.vo.FeeRulesVo;
import com.lanswon.qzsmk.vo.SellCardVo;
import com.lanswon.qzsmk.vo.VoucherVo;

/**
 * 换卡controller
 * @author guyawei
 *
 */
@Controller
@RequestMapping(value="/exchangeCard")
public class ExchangeCardController {

	
	@Autowired
	ChangeCardService ccs;
	
	@Autowired
	AppFeeService afs;
	
	/**
	 * 链接换卡页面
	 * @return
	 */
	@RequiresPermissions("exchangeCard:index")
	@RequestMapping(value="/goExchangeCard",name="换卡,exchangeCard:index",method={RequestMethod.GET,RequestMethod.POST})
	public String goExchangeCard(){
		
		return"ExchangeCard";
	}	
	
	/**
	 * 获取需要换卡信息
	 * @param cci
	 */
	@RequiresPermissions("exchangeCard:getExchangeCardInfo")
	@RequestMapping(value="/getExchangeCardInfo",name="换卡获取换卡信息,exchangeCard:getExchangeCardInfo",method=RequestMethod.POST)
	@ResponseBody
	public DwzPage<ChangeCardInfo> getExchangeCardInfo(ChangeCardInfo cci,Integer pageNum,Integer numPerPage){
//		if(cci.getNumPerPage()==null){//页面加载时执行
//			cci.setCurrentPage(1);
//			cci.setNumPerPage(20);
//		}else {//页面分页
//			if(cci.getCurrentPage()<=1||pageCount==0){
//				cci.setCurrentPage(1);
//			}else if(cci.getCurrentPage()>=pageCount&&pageCount!=0){
//				cci.setCurrentPage(pageCount);
//			}
//		}
		System.out.println(pageNum+","+numPerPage+",");
		if(pageNum==null&&numPerPage==null){
			pageNum=1;
			numPerPage=20;	
		}
		cci.setCurrentPage(pageNum);
		cci.setNumPerPage(20);
		cci.setStartLine((cci.getCurrentPage()-1)*cci.getNumPerPage());
		DwzPage<ChangeCardInfo> dPage=ccs.queryChangeCard(cci);
		System.out.println(cci);
		return dPage;
	}
	
	/**
	 * 获取换卡登记页面需要的详细信息并跳转换卡登记页面
	 * @param exchangeCard
	 * @param isRead
	 * @return
	 * @throws ApplicationException 
	 */
	@RequiresPermissions("exchangeCard:goExchangeRegister")
	@RequestMapping(value="/goExchangeRegister",name="换卡获取换卡登记页面需要的详细信息,exchangeCard:goExchangeRegister",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> goExchangeRegister(String[] exchangeCard,String isRead,String balance,HttpServletRequest request) throws ApplicationException{
		System.out.println(isRead+";"+balance);
		for(String str:exchangeCard){
			System.out.println(str);
		}
		ChangeCardInfo cci=new ChangeCardInfo();
		cci.setCardPName(exchangeCard[1]);
		cci.setPaperTypeName(exchangeCard[3]);
		cci.setPaperNo(exchangeCard[4]);
		cci.setCardTypeName(exchangeCard[2]);
		cci.setIsRead(isRead);
		Map<String, Object> ExchangeInfo=null;
		Map<String, Object> map=new HashMap<>();//跳转用
		Map<String,Map<String,Object>> info=ccs.queryPersonalByPaperNo(cci);
		if(info.containsKey("Anonymous_Card")){//不记名卡
			ExchangeInfo=info.get("Anonymous_Card");
			if(isRead.equals(CardContants.CARD_IS_READ)){
				ExchangeInfo.put("blance", balance);//余额
			}else{
				ExchangeInfo.put("blance", "");//余额
			}
			ExchangeInfo.put("cardNo", exchangeCard[0]);//卡号
			request.getSession().removeAttribute("ExchangeInfo");
			request.getSession().setAttribute("ExchangeInfo", ExchangeInfo);
			map.put("statusCode", "200");
			map.put("callbackType", "forward");
			map.put("forwardUrl", "reissueCard/exchangeRegisterBlance");//post请求
		}else {//记名卡
			ExchangeInfo=info.get("Registered_Card");
//			FeeRulesVo vo=new FeeRulesVo();
//			vo.setCardpName(exchangeCard[1]);
//			vo.setFeeType("1");//工本费代码
//			FeeRules FeeRules=afs.queryFeeRulesByCardPCodeAndFeeType(vo);
//			if(FeeRules!=null){
//				ExchangeInfo.put("fee",FeeRules.getFeeBln());
//			}
			if(isRead.equals(CardContants.CARD_IS_READ)){
				ExchangeInfo.put("blance", balance);//余额
			}else{
				ExchangeInfo.put("blance", "");//余额
			}
			ExchangeInfo.put("cardNo", exchangeCard[0]);//卡号
			ExchangeInfo.put("cardpName", exchangeCard[1]);//卡产品
			ExchangeInfo.put("paperType", exchangeCard[3]);
			request.getSession().removeAttribute("ExchangeInfo");
			request.getSession().setAttribute("ExchangeInfo", ExchangeInfo);
			map.put("statusCode", "200");
			map.put("callbackType", "forward");
			map.put("forwardUrl", "exchangeCard/exchangeRegister");//post请求
		}
		
		return map;
	}
	
	/**
	 * 链接换卡登记页面(记名卡)
	 * @return
	 */
	@RequiresPermissions("exchangeCard:exchangeRegister")
	@RequestMapping(value="/exchangeRegister",name="换卡登记页面,exchangeCard:exchangeRegister",method=RequestMethod.POST)
	public String exchangeRegister(){
		
		return"ExchangeRegister";
	}
	
	/**
	 * 链接换卡登记页面(不记名卡)
	 * @return
	 */
	@RequiresPermissions("exchangeCard:exchangeRegisterBlance")
	@RequestMapping(value="/exchangeRegisterBlance",name="换卡登记页面(不记名卡),exchangeCard:exchangeRegisterBlance",method=RequestMethod.POST)
	public String exchangeRegisterBlance(){
		
		return"ExchangeRegisterBlankCard";
	}
	
	/**
	 * 换卡登记页面获取详细信息
	 * @param request
	 * @return
	 */
	@RequiresPermissions("exchangeCard:getExchangeInfo")
	@RequestMapping(value="/getExchangeInfo",name="换卡登记页面获取详细信息,exchangeCard:getExchangeInfo",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getExchangeInfo(HttpServletRequest request){
		@SuppressWarnings("unchecked")
		Map<String,Object> map=(Map<String, Object>) request.getSession().getAttribute("ExchangeInfo");
		//request.getSession().removeAttribute("ExchangeInfo");
		return map;
	}
	/**
	 * 提交换卡登记信息(记名卡)
	 * @param cci
	 * @param exchangeCardTime
	 * @param getCardTime
	 * @return
	 * @throws ParseException
	 * @throws ApplicationException 
	 */
	@RequiresPermissions("exchangeCard:commitExchangeInfo")
	@RequestMapping(value="/commitExchangeInfo",name="换卡提交,exchangeCard:commitExchangeInfo",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> commitExchangeInfo(ChangeCardInfo cci,HttpServletRequest request) throws ParseException, ApplicationException{
		System.out.println(cci);
		Map<String,Object> map=new HashMap<>();//跳转用
		Map<String, Object> infos=ccs.commitChangeCardInfo(cci);
		if(infos.containsKey(CardContants.CARD_IS_READ)){//可读
			if(cci.getSceneSendCard().equals(CardContants.SCENE_SEND_CARD_IS)){
				//当场换卡
				SellCardVo scv=(SellCardVo) infos.get(CardContants.CARD_IS_READ);
				request.getSession().setAttribute("scv", scv);
				map.put("statusCode", "200");
				map.put("flag", 1);
				map.put("callbackType", "forward");
				map.put("forwardUrl", "issueCardSingle/getCardRegister");//post请求	跳转发卡页面
			}else{
				VoucherVo vv=(VoucherVo) infos.get(CardContants.CARD_IS_READ);
				request.getSession().setAttribute("vo", vv);
				request.getSession().setAttribute("type", "changeCardRegister");
				map.put("max", "true");
		        map.put("title", "可读换卡凭证");
		        map.put("rel", "exchange");
				map.put("statusCode", "200");
		        map.put("url", "printCredentials/toHairpinPdfView");
		        //20181206 hgl 增加以下3行
		        map.put("jumpToTile", "换卡");
		        map.put("jumpToTabid", "exchangeCard");
		        map.put("jumpToUrl", "exchangeCard/goExchangeCard");
			}	
		}else{//不可读跳转凭证页面
			if(cci.getSceneSendCard().equals(CardContants.SCENE_SEND_CARD_IS)){
				//当场换卡
				SellCardVo scv=(SellCardVo) infos.get(CardContants.CARD_NO_READ);
				request.getSession().setAttribute("scv", scv);
				map.put("statusCode", "200");
				map.put("flag", 1);
				map.put("callbackType", "forward");
				map.put("forwardUrl", "issueCardSingle/getCardRegister");//post请求	跳转发卡页面
			}else{
				VoucherVo vv=(VoucherVo) infos.get(CardContants.CARD_NO_READ);
				System.out.println(vv.getCertNo());
				request.getSession().setAttribute("vo", vv);
				request.getSession().setAttribute("type", "changeCardRegister");
				map.put("max", "true");
		        map.put("title", "不可读换卡凭证");
		        map.put("rel", "exchange");
				map.put("statusCode", "200");
		        map.put("url", "printCredentials/toHairpinPdfView");
		        //20181206 hgl 增加以下3行
		        map.put("jumpToTile", "换卡");
		        map.put("jumpToTabid", "exchangeCard");
		        map.put("jumpToUrl", "exchangeCard/goExchangeCard");
			}
		}
		return map;
	}
	
	/**
	 * 提交换卡登记信息(不记名卡)
	 * @param cci
	 * @param exchangeCardTime
	 * @param getCardTime
	 * @return
	 * @throws ParseException
	 */
//	@RequestMapping(value="/commitExchangeInfoBlance",method=RequestMethod.POST)
//	@ResponseBody
//	public  Map<String, Object> commitExchangeInfoBlance(ChangeCardInfo cci,String exchangeCardTime,String getCardTime) throws ParseException{
//		cci.setChangeCardDate(new SimpleDateFormat("yyyy-dd-MM").parse(exchangeCardTime));
//		cci.setGetCardDate(new SimpleDateFormat("yyyy-dd-MM").parse(getCardTime));
//		System.out.println(cci+";"+exchangeCardTime+";"+getCardTime);
//		Map<String, Object> map=ccs.commitChangeCardInfo(cci);
//		if(map.containsKey(CardContants.CARD_IS_READ)){//可读跳转不记名发卡页面
//			//doSomethings
//			map.put("statusCode", "200");
//			map.put("message", "操作成功");
//			map.put("callbackType", "forward");
//			map.put("forwardUrl", "reissueCard/exchangeRegister");//post请求	跳转发卡页面
//		}else{//不可读跳转凭证页面
//			Cert cert=(Cert) map.get(CardContants.CARD_NO_READ);
//		}
//		return map;
//	}
}
